package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.SaleListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.service.SaleListService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * description
 *
 * @author SZ
 * @date 2022/9/26/8:34
 */
@RestController
@RequestMapping("/saleListGoods")
public class SaleListGoodsController {
    @Autowired
    private SaleListGoodsService saleListGoodsService;
    @Autowired
    private SaleListService saleListService;
    @PostMapping("/save")
    public ServiceVO save(SaleList saleList, String saleListGoodsStr, HttpSession session){
        User user = (User) session.getAttribute ("currentUser");
        Integer userId = user.getUserId ();
        saleList.setUserId (userId);
        Gson gson = new Gson ();
        List list = gson.fromJson (saleListGoodsStr, List.class);
        List<SaleListGoods> saleListGoodsList = new ArrayList<> ();
        for (Object o : list) {
            String json = gson.toJson (o);
            SaleListGoods saleListGoods = gson.fromJson (json, SaleListGoods.class);
            saleListGoodsList.add (saleListGoods);
        }
        return saleListGoodsService.save(saleList,saleListGoodsList);
    }
    @PostMapping("/list")
    public List<SaleListGoods> list(String saleNumber, Integer customerId, Integer state, String sTime,String eTime){
        return saleListService.getList(saleNumber,customerId,state,sTime,eTime);
    }
    @PostMapping("/goodsList")
    public List<SaleListGoods> getGoodsList(Integer saleListId){
        return saleListGoodsService.getGoodsList(saleListId);
    }
    @PostMapping("/delete")
    public ServiceVO delete(Integer saleListId){
        return saleListService.delete(saleListId);
    }
}
